package zmh.web.controller;

import org.springframework.aop.framework.AopContext;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangminhang1
 * @date 2022-1-14 15:18:02
 */
@RestController
public class MyController {
    final ExecutorService executorService = Executors.newFixedThreadPool(5);

    private Random random = new SecureRandom();

    @GetMapping("/hello")
    public String getStr(String arg){
        final MyController myController = (MyController) AopContext.currentProxy();
        return "hello"+random.nextInt(1000)+"arg="+myController.getMap().get(arg);
    }

    @Cacheable(value = "caffeineCache")
    public Map<String,Object> getMap(){
        Map<String,Object> map = new HashMap<>(16);
        executorService.execute(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            map.put("1",1);
            map.put("2",2);
            map.put("3",3);
            map.put("4",4);
            map.put("5",6);
        });

        System.out.println("不是从缓存中获取");
        return map;
    }
}
